package io.fredia.femicro.auth.client.config;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.Feign;
import io.fredia.femicro.auth.client.interceptor.OkHttpTokenInterceptor;
import okhttp3.ConnectionPool;

/**
 * okhttp 连接池
 * 
 * @author : Fredia
 * @since : 2018年3月13日
 * @version : v1.0.0
 */
@AutoConfigureBefore(FeignAutoConfiguration.class)
@Configuration
@ConditionalOnClass(Feign.class)
public class FeignOkHttpConfig {

	@Autowired
	OkHttpTokenInterceptor okHttpLoggingInterceptor;

	private int feignOkHttpReadTimeout = 60;
	private int feignConnectTimeout = 60;
	private int feignWriteTimeout = 120;

	@Bean
	public okhttp3.OkHttpClient okHttpClient() {
		return new okhttp3.OkHttpClient.Builder().readTimeout(feignOkHttpReadTimeout, TimeUnit.SECONDS)
				.connectTimeout(feignConnectTimeout, TimeUnit.SECONDS).writeTimeout(feignWriteTimeout, TimeUnit.SECONDS)
				.connectionPool(new ConnectionPool()).addInterceptor(okHttpLoggingInterceptor).build();
	}
}
